package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.dao.system.RoleDao;
import cn.itcast.dao.system.UserDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

//购销合同的服务实现类
@Service
public class ContractServiceImpl implements ContractService  {

    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ExtCproductDao extCproductDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;
    //购销合同的分页列表
    @Override
    public PageInfo<Contract> findByPage(ContractExample contractExample, int pageNum, int pageSize) {
        //1. 设置开始页与页面大小
        PageHelper.startPage(pageNum,pageSize);
        //2. 设置所有
        List<Contract> list = contractDao.selectByExample(contractExample);
        //3 /.构建一个PageInfo
        PageInfo<Contract> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    //根据条件查询全部的购销合同
    @Override
    public List<Contract> findAll(ContractExample contractExample) {
        List<Contract> contractList = contractDao.selectByExample(contractExample);
        return contractList;
    }

    //根据id查找购销合同
    @Override
    public Contract findById(String id) {
        Contract contract = contractDao.selectByPrimaryKey(id);
        return contract;
    }

    //添加购销合同
    @Override
    public void save(Contract contract) {
        //设置主键
        contract.setId(UUID.randomUUID().toString());
        contract.setCreateTime(new Date());
        contract.setUpdateTime(new Date());
        contract.setState(0);
        contractDao.insertSelective(contract);
    }

    //更新购销合同
    @Override
    public void update(Contract contract) {
      contract.setUpdateTime(new Date());
       contractDao.updateByPrimaryKeySelective(contract);
    }

    //根据主键删除购销合同
    @Override
    public void delete(String id) {
        //先遍历所属该合同的所有附件
        ExtCproductExample extCproductExample = new ExtCproductExample();
        extCproductExample.createCriteria().andContractIdEqualTo(id);
        List<ExtCproduct> extCproductList = extCproductDao.selectByExample(extCproductExample);
        //删除该合同的所有附件
        for (ExtCproduct extCproduct : extCproductList) {
            extCproductDao.deleteByPrimaryKey(extCproduct.getId());
        }
        //遍历该合同下的所有货物
        ContractProductExample contractProductExample = new ContractProductExample();
        contractProductExample.createCriteria().andContractIdEqualTo(id);
        List<ContractProduct> contractProductList = contractProductDao.selectByExample(contractProductExample);
        //删除该合同的所有货物
        for (ContractProduct contractProduct : contractProductList) {
            contractProductDao.deleteByPrimaryKey(contractProduct.getId());
        }
        contractDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Contract> findPageByDeptId(String deptId, Integer pageNum, Integer pageSize) {
        //1. 设置开始页与页面大小
        PageHelper.startPage(pageNum,pageSize);
        //2. 设置所有
        List<Contract> list = contractDao.findByDeptId(deptId);
        //3 /.构建一个PageInfo
        PageInfo<Contract> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<User> findByRoleName(String name) {
       List<String> userIdList = roleDao.findByName(name);
       List<User> userList = new ArrayList<>();
       for (String id : userIdList) {
           User user = userDao.findById(id);
           userList.add(user);
       }
        return userList;
    }


}
